import ajax, { ListResult, toOptions, getQuery } from '../ajax';

export function getList(data: any) {
  return ajax.post<ListResult>('/ControlPanels/Applications/Query', data);
}

export async function getAll() {
  let pdata = getQuery('applicationId');
  let data = await getList(pdata);
  return toOptions(data.items, 'applicationName', 'applicationId');
}

export function updateEntity(data: any) {
  if (data.applicationId) {
    return ajax.put<any>('/ControlPanels/Applications', data);
  } else {
    return ajax.post<any>('/ControlPanels/Applications', data);
  }
}

export function deleteEntity(id: number) {
  let deleteCommand = { id: id };
  return ajax.put<any>(
    '/ControlPanels/Applications/Actions/Delete',
    deleteCommand
  );
}

export function getAppRoles(appId: number) {
  let data = getQuery('roleId');
  return ajax.post<ListResult>('/ControlPanels/Roles/Query', data);
}

export function getAppGroups(appId: number) {
  let data = getQuery('groupId');
  return ajax.post<ListResult>(`/ControlPanels/Groups/Query`, data);
}

export async function getAllUsers(appId: number, tenantId: number) {
  let data = await ajax.get(
    `/ControlPanels/Applications/${appId}/Tenants/${tenantId}/Users`
  );
  return toOptions(data, 'userName', 'userId');
}
